package net.springmvc13_14.controller;

import net.springmvc13_14.entity.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stu")
public class StuController {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello";
    }

    //    @RequestMapping(value = "/hh",method = RequestMethod.POST)
    @PostMapping("/hh")
    public String hh() {
        System.out.println("hh");
        return "hh";
    }

    @GetMapping("/data")
    @ResponseBody
    public String data(@RequestParam(value = "name") String name,
                       @RequestParam(value = "age", required = false, defaultValue = "10") String age) {
        System.out.println(name + " " + age);
        return "hh";
    }

    @GetMapping("/data1")
    @ResponseBody
    public String data1(@RequestParam(value = "name") List<String> names) {
        System.out.println(names);
        return "hh";
    }

    @GetMapping("/data2")
    @ResponseBody
    public String data2(Stu stu) {
        System.out.println(stu);
        return "hh";
    }

    @GetMapping("/data3/{id1}")
    @ResponseBody
    public String data3(@PathVariable(value = "id1") int id) {
        System.out.println(id);
        return "hh";
    }

    @GetMapping("/data4")
    @ResponseBody
    public String data4(@RequestBody Stu stu) {
        System.out.println(stu);
        return "hh";
    }

    @GetMapping("/data5")
    @ResponseBody
    public String data5(@CookieValue(value = "JSESSIONID") String cookie) {
        System.out.println(cookie);
        return "hh";
    }

    @GetMapping("/data6")
    @ResponseBody
    public String data6(@RequestHeader(value = "host") String host) {
        System.out.println(host);
        return "hh";
    }

    // request级别 共享域
    @GetMapping("/data7")
    public String data7(Model model) {
        model.addAttribute("model", "model数据");
        return "hh";
    }

    @GetMapping("/data8")
    public String data8(ModelMap model) {
        model.addAttribute("model", "ModelMap数据");
        return "hh";
    }

    @GetMapping("/data9")
    public String data9(Map<String, Object> map) {
        map.put("model", "Map数据");
        return "hh";
    }

    @GetMapping("/data10")
    public String data10(HttpServletRequest request) {
        request.setAttribute("model", "HttpServletRequest数据");
        return "hh";
    }

    @GetMapping("/data11")
    public ModelAndView data10(ModelAndView modelAndView) {
        modelAndView.addObject("model", "ModelAndView数据");
        modelAndView.setViewName("hh");
        return modelAndView;
    }

    // session 级别共享域
    @GetMapping("/data12")
    public String data12(HttpSession session) {
        session.setAttribute("model", "HttpSession数据");
        return "hh";
    }

    @Autowired
    private ServletContext servletContext;

    @GetMapping("/data13")
    public String data13() {
        servletContext.setAttribute("model", "ServletContext数据");
        return "hh";
    }

    // 重定向
    @GetMapping("/redirect")
    public String redirect() {
//        request.getRequestDispatcher("/xx").forward(request, response);
//        response.sendRedirect("/xxx");
        return "redirect:/index.jsp";
    }

    // 转发
    @GetMapping("/forward")
    public String forward() {
        return "forward:/stu/data13";
    }
}
